package gtug.duesseldorf.client.ui;

import gtug.duesseldorf.client.Wave_webclient;

import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.TextBox;

public class InputBox extends Composite {

	private final Wave_webclient m_client;

	private final TextBox m_tfInput;

	public InputBox(Wave_webclient client) {
		m_client = client;

		FlexTable t = new FlexTable();
		t.setWidth("100%");
		t.setCellSpacing(5);
		initWidget(t);

		t.setWidget(0, 0, new HTML("Input: "));

		m_tfInput = new TextBox();
		m_tfInput.setWidth("100%");
		t.setWidget(0, 1, m_tfInput);
		t.getFlexCellFormatter().setWidth(0, 1, "100%");

		m_tfInput.addKeyPressHandler(new KeyPressHandler() {

			@Override
			public void onKeyPress(KeyPressEvent event) {

				// return pressed?
				if (13 == event.getCharCode()) {
					String txt = m_tfInput.getText();
					m_tfInput.setText(null);

					if (txt != null && txt.length() != 0) {
						m_client.sendText(txt);
					}
				}

			}
		});
	}

	public void setFocus() {
		m_tfInput.setFocus(true);
	}
}
